home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d7 / jmodm308.arc / JMODEM_F.C < prev    next >
Text File  |  1991-01-02  |  23KB  |  491 lines

  1. /****************************************************************************/
  2. /*   FILE JMODEM_F.C                                                        */
  3. /*   Created 11-JAN-1990                    Richard B. Johnson              */
  4. /*                                          405 Broughton Drive             */
  5. /*                                          Beverly, Massachusetts 01925    */
  6. /*                                          BBS (508) 922-3166              */
  7. /*                                                                          */
  8. /*   screen();         All screen output procedures. Uses INT 10H under     */
  9. /*                     MS-DOS.                                              */
  10. /*                                                                          */
  11. /*   disp();           Displays a "USAGE" message                           */
  12. /*                                                                          */
  13. /*   These routines are absolutely not necessary and could be replaced      */
  14. /*   with _printf() statements. They are used to make the pretty screens    */
  15. /*   and overlapping windows that the PC community has grown to expect.     */
  16. /*                                                                          */
  17. /****************************************************************************/
  18. #define  SCREEN                                  /* For var len param list  */
  19. #include <stdio.h>
  20. #include <dos.h> 
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "screen.h"
  24. #include "jmodem.h"
  25.  
  26. short sav_par[boxes * param];                     /* Save row/columns       */
  27. short *buffer[boxes];                             /* Pointers for  boxes    */
  28. short last_box = 0;                               /* Last box on the screen */
  29. short start_txt;                                  /* Text starts in box     */
  30. short start_row;                                  /* Starting row of box    */
  31. short start_col;                                  /* Starting column of box */
  32. short end_row;                                    /* Ending row of box      */
  33. short end_col;                                    /* Ending column of box   */
  34.  
  35. byte abrt[]  = "Aborted!";                        /* Five status messages   */
  36. byte okay[]  = "Okay ";
  37. byte retry[] = "Retry";
  38. byte done[]  = "Done!";
  39. byte flow[]  = "Flow ";
  40.  
  41. char info[] = "Usage:\r\n"\
  42.               "JMODEM R1 filename\r\n"\
  43.               "       ||___ COM port (1..4)\r\n"\
  44.               "       |____ [R]eceive, [S]end";
  45.  
  46. byte malfail[] = "Memory allocation failed!";
  47.  
  48. char *signon[] = {
  49.                  "     J M O D E M",
  50.                  "File transfer protocol",
  51.                  "     "VERS
  52.                  };
  53.  
  54. char *sta_blk[] = {
  55.                   "   Block :" ,
  56.                   "  Length :" ,
  57.                   "   Bytes :" ,
  58.                   "Rate CPS :" ,
  59.                   "  Status :" ,
  60.                   "Synchronizing ...", 
  61.                   "  Receiving file ",
  62.                   "Transmitting file"
  63.                   };
  64.  
  65. char *fil_blk[] = {
  66.                   "Opening file ",
  67.                   "Can't open the file!",
  68.                   "Open okay",
  69.                   "File exists, renaming to ",
  70.                   "Can't create the file!",
  71.                   };
  72. /****************************************************************************/
  73. /*                                C O D E                                   */
  74. /****************************************************************************/
  75. void screen (short function, SYS *sys)
  76. {
  77.     union REGS bios;                              /* For int 10H            */
  78.     register const ATTRIB *attr;                  /* For attribute table    */
  79.     register const short *index;                  /* Array pointer          */
  80.     short page;                                   /* Box number             */
  81.     short i;
  82.  
  83.     bios.x.ax =                                   /* Initialize             */
  84.     bios.x.bx =
  85.     bios.x.cx =
  86.     bios.x.dx = 0;
  87.  
  88.     attr = attribute + last_box;                  /* Default attribute      */
  89.     switch (function)
  90.     {
  91.         case SCR_SGN:                             /* Sign-on message        */
  92.         {
  93.             page=0;                               /* Box number             */
  94.             kill_curs(&bios);
  95.             attr      = attribute + page;
  96.             index     = box_loc + (page * boxes);
  97.             start_row = *index++;
  98.             start_col = *index++;
  99.             end_row   = *index++;
  100.             end_col   = *index;
  101.             start_txt = start_row + 1;
  102.             set_box (page     ,                   /* Page                   */
  103.                      attr->win,                   /* Screen attribute       */
  104.                      start_row,                   /* Start row              */
  105.                      start_col,                   /* Start column           */
  106.                      end_row  ,                   /* End row                */
  107.                      end_col  ,                   /* End column             */
  108.                      sav_par  ,
  109.                      buffer   ,
  110.                      &bios  );
  111.             for (i = 0; i<3; i++)
  112.             {
  113.                 set_curs (start_txt++,start_col + 4,&bios);
  114.                 write_str(signon[i],attr->txt,&bios);
  115.             }
  116.             last_box = page;
  117.             break;
  118.         }
  119.     case SCR_FIL:                                 /* File screen            */
  120.         {
  121.             page=1;
  122.             attr      = attribute + page;
  123.             index     = box_loc + (page * boxes);
  124.             start_row = *index++;
  125.             start_col = *index++;
  126.             end_row   = *index++;
  127.             end_col   = *index;
  128.             start_txt = start_row + 1;
  129.             set_box (page     ,                   /* Page                   */
  130.                      attr->win,                   /* Screen attribute       */
  131.                      start_row,                   /* Start row              */
  132.                      start_col,                   /* Start column           */
  133.                      end_row  ,                   /* End row                */
  134.                      end_col  ,                   /* End column             */
  135.                      sav_par  ,
  136.                      buffer   ,
  137.                      &bios  );
  138.             set_curs (start_txt++,start_col + 3,&bios);
  139.             write_str(fil_blk[0],attr->txt,&bios);
  140.             write_str(sys->s_txt,attr->txt,&bios);
  141.             last_box=page;
  142.             break;
  143.         }
  144.     case SCR_FNF:                                 /* File not found         */
  145.         {
  146.             set_curs (start_txt++ ,start_col + 3, &bios);
  147.             write_str(fil_blk[1],attr->txt,&bios);
  148.             break;
  149.         }
  150.     case SCR_FOK:                                 /* File found okay        */
  151.         {
  152.             set_curs (start_txt++, start_col + 3, &bios);
  153.             write_str(fil_blk[2],attr->txt,&bios);
  154.             if (sys)
  155.             {
  156.                 strcpy(info,"File size = ");
  157.                 ltoa (sys->s_byt,&info[12],10);
  158.                 set_curs (start_txt-1, start_col + 38, &bios);
  159.                 write_str(info,attr->txt,&bios);
  160.             }
  161.             break;
  162.         }
  163.     case SCR_STA:                                 /* Status block           */
  164.         {
  165.             page      = 2;
  166.             attr      = attribute + page;
  167.             index     = box_loc + (page * boxes);
  168.             start_row = *index++;
  169.             start_col = *index++;
  170.             end_row   = *index++;
  171.             end_col   = *index;
  172.             start_txt = start_row + 1;
  173.             set_box (page     ,                   /* Page                   */
  174.                      attr->win,                   /* Screen attribute       */
  175.                      start_row,                   /* Start row              */
  176.                      start_col,                   /* Start column           */
  177.                      end_row  ,                   /* End row                */
  178.                      end_col  ,                   /* End column             */
  179.                      sav_par  ,
  180.                      buffer   ,
  181.                      &bios  );
  182.  
  183.             for (i=0; i<6; i++)
  184.             {
  185.                 set_curs (start_txt+i,start_col + 4,&bios);
  186.                 write_str(sta_blk[i],attr->txt,&bios);
  187.             }
  188.             last_box = page;
  189.             break;
  190.         }
  191.    case SCR_FRN:                                  /* File renamed           */
  192.         {
  193.             set_curs (start_txt++, start_col + 3, &bios);
  194.             write_str(fil_blk[3],attr->txt,&bios);
  195.             write_str(sys->s_txt,attr->txt,&bios);
  196.             break;
  197.         }
  198.    case SCR_FCR:                                  /* File created           */
  199.         {
  200.             set_curs (start_txt++, start_col + 3, &bios);
  201.             write_str(fil_blk[4],attr->txt,&bios);
  202.             break;
  203.         }
  204.    case SCR_SYR:                                  /* Sync receive           */
  205.         {
  206.             start_txt = start_row + 1;
  207.             set_curs (start_txt + 5, start_col + 4, &bios);
  208.             write_str(sta_blk[6],attr->txt | 0x8000 ,&bios);
  209.             break;
  210.         }
  211.    case SCR_SYT:                                  /* Sync transmit          */
  212.         {
  213.             start_txt = start_row + 1;
  214.             set_curs (start_txt + 5, start_col + 4, &bios);
  215.             write_str(sta_blk[7],attr->txt | 0x8000,& bios);
  216.             break;
  217.         }
  218.     case SCR_SYS:
  219.         {
  220.             itoa(sys->s_blk,info,10);             /* Block number           */
  221.             set_curs (start_txt, start_col + 15, &bios);
  222.             write_str(info,attr->txt,& bios);
  223.             memset(info,0x20,0x08);               /* Fixed length string    */
  224.             info[0x07] = 0x00;                    /* Set a null             */
  225.             itoa(sys->s_len,info,10);             /* Block length           */
  226.             *(strchr(info,0x00)) = 0x20;          /* Fill in the NULL       */
  227.             set_curs (start_txt + 1, start_col + 15, &bios);
  228.             write_str(info,attr->txt, &bios);
  229.             ltoa(sys->s_byt,info,10);             /* Total bytes            */
  230.             set_curs (start_txt + 2, start_col + 15, &bios);
  231.             write_str(info,attr->txt, &bios);
  232.             memset(info,0x20,0x08);               /* Fixed length string    */
  233.             info[0x07] = 0x00;                    /* Set a null             */
  234.             itoa(sys->s_cps,info,10);             /* Speed, cps             */
  235.             *(strchr(info,0x00)) = 0x20;          /* Fill in the NULL       */
  236.             set_curs (start_txt + 3, start_col + 15, &bios);
  237.             write_str(info,attr->txt, &bios);
  238.         }                                         /* Fall through, no break */
  239.     case SCR_FLG:
  240.         {
  241.             set_curs (start_txt + 4, start_col + 15, &bios);
  242.             write_str(sys->s_sta,attr->txt, &bios);
  243.             break;
  244.         }
  245.     case SCR_END:
  246.         {
  247.             do
  248.             {
  249.                 end_box (last_box, sav_par, buffer, &bios);
  250.             }   while (last_box--);
  251.             restore_curs(&bios);
  252.             break;
  253.         }
  254.     }
  255.     return;
  256. }
  257. /****************************************************************************/
  258. /*  Save screen contents in a buffer obtained from _malloc. Write a border  */
  259. /*  and screen attributes to saved screen location.  Record the address of  */
  260. /*  the buffer so the screen contents may be restored. Global *buffer[] is  */
  261. /*  used to save the pointers.                                              */
  262. /****************************************************************************/
  263. void set_box (short page ,                        /* Box number             */
  264.              word screen,                         /* Screen attribute       */
  265.              short start_row,                     /* Start row of border    */
  266.              short start_col,                     /* Start column of border */
  267.              short end_row,                       /* End row of border      */
  268.              short end_col,                       /* End column of border   */
  269.              register short *sav_par,
  270.              register short *buffer[],
  271.              union REGS *bios)
  272. {
  273.     word putscr;
  274.     short sav_col;
  275.     short sav_row;
  276.     short row;
  277.     short col;
  278.     buffer[page] = (short *)
  279.                    allocate_memory (              /* Get pointer to memory  */
  280.                    ((end_row - start_row)
  281.                   * (end_col - start_col))
  282.                   *  sizeof (short) );
  283.     if (!buffer[page])
  284.         return;
  285.  
  286.     get_curs(bios);                               /* Get cursor position    */
  287.     sav_row    = (short) bios->h.dh;              /* Save cursor row        */
  288.     sav_col    = (short) bios->h.dl;              /* Save cursor column     */
  289.     sav_par   += (page * param);                  /* Calculate index once   */
  290.     *sav_par++ = screen;
  291.     *sav_par++ = start_row;
  292.     *sav_par++ = start_col;
  293.     *sav_par++ = end_row;
  294.     *sav_par++ = end_col;
  295.     *sav_par++ = sav_row;
  296.     *sav_par   = sav_col;
  297.  
  298.     for (row = start_row; row < end_row; row++)
  299.     {
  300.         for (col = start_col; col < end_col; col++)
  301.         {
  302.             set_curs (row,col,bios);                   /* Set cursor pos    */
  303.             *buffer[page]++ = get_char_atr(bios);      /* Save char/attr    */
  304.             putscr = screen | 0x20;                    /* default (else)    */
  305.             if (row == start_row || row == end_row-1)  /* Top and bottom    */
  306.                 putscr = screen | 205;
  307.             if (col == start_col || col == end_col-1)  /* Right and left    */
  308.                 putscr = screen | 186;
  309.             if (row == start_row && col == start_col)  /* NW corner         */
  310.                 putscr = screen | 201;
  311.             if (row == start_row && col == end_col-1)  /* NE corner         */
  312.                 putscr = screen | 187;
  313.             if (row == end_row-1 && col == start_col)  /* SW corner         */
  314.                 putscr = screen | 200;
  315.             if (row == end_row -1 && col == end_col-1) /* SE corner         */
  316.                 putscr = screen | 188;
  317.             set_char_atr (putscr,bios);                /* Write to screen   */
  318.         }
  319.     }
  320.     return;
  321. }
  322. /****************************************************************************/
  323. /*  Restore the screen contents saved in memory pointed to by *buffer[].    */
  324. /****************************************************************************/
  325. void end_box (short page,
  326.               register short *sav_par,
  327.               register short *buffer[],
  328.               union REGS  *bios)
  329. {
  330.     short start_row;
  331.     short start_col;
  332.     short end_row;
  333.     short end_col;
  334.     short row;
  335.     short col;
  336.     short sav_row;
  337.     short sav_col;
  338.  
  339.     sav_par  += (page * param);                   /* Calculate index once   */
  340.     sav_par++;                                    /* Bypass screen entry    */
  341.     start_row = *sav_par++;
  342.     start_col = *sav_par++;
  343.     end_row   = *sav_par++;
  344.     end_col   = *sav_par++;
  345.     sav_row   = *sav_par++;
  346.     sav_col   = *sav_par;
  347.  
  348.     buffer[page] -= ( (end_row - start_row)       /* Get buffer start       */
  349.                * (end_col - start_col) );
  350.  
  351.     for (row = start_row; row < end_row; row++)
  352.     {
  353.         for (col = start_col; col < end_col; col++)
  354.         {
  355.             set_curs (row,col,bios);              /* Set cursor pos         */
  356.             set_char_atr(*buffer[page]++,bios);   /* Restore screen         */
  357.         }
  358.     }
  359.     free(buffer[page]);                           /* Free allocated memory  */
  360.     set_curs (sav_row,sav_col,bios);              /* Restore cursor         */
  361.     return;
  362. }
  363. /****************************************************************************/
  364. /*                      Set Cursor to row, column                           */
  365. /****************************************************************************/
  366. void set_curs (short row, short col, register union REGS *bios)
  367. {
  368.     bios->h.ah=(byte) 0x02;                       /* Set cursor function    */
  369.     bios->h.dh=(byte) row;                        /* Row                    */
  370.     bios->h.dl=(byte) col;                        /* Column                 */
  371.     bios->h.bh=(byte) 0x00;                       /* Page 0                 */
  372.     int86(VIDEO, bios, bios);                     /* Use for in and out     */
  373.     return;
  374. }
  375. /****************************************************************************/
  376. /*                          Get the cursor position                         */
  377. /****************************************************************************/
  378. void get_curs(register union REGS *bios)
  379. {
  380.     bios->h.ah=(byte) 0x03;                       /* Get cursor function    */
  381.     bios->h.bh=(byte) 0x00;                       /* Page 0                 */
  382.     int86(VIDEO, bios, bios);                     /* Use for in and out     */
  383.     return;
  384. }
  385. /****************************************************************************/
  386. /*                           Turn off the cursor                            */
  387. /****************************************************************************/
  388. void kill_curs(register union REGS *bios)
  389. {
  390.     bios->h.ah=(byte) 0x01;                       /* Set cursor function    */
  391.     bios->h.bh=(byte) 0x00;                       /* Page 0                 */
  392.     bios->x.cx=(word) 0xFFFF;                     /* Kill the cursor        */
  393.     int86(VIDEO, bios, bios);                     /* Use for in and out     */
  394.     return;
  395. }
  396. /****************************************************************************/
  397. /*                         Restore the cursor type                          */
  398. /****************************************************************************/
  399. void restore_curs(register union REGS *bios)
  400. {
  401.     bios->h.ah=(byte) 0x01;                       /* Get cursor function    */
  402.     bios->h.bh=(byte) 0x00;                       /* Page 0                 */
  403.     bios->x.cx=(word) 0x0607;                     /* Restore the cursor     */
  404.     int86(VIDEO, bios, bios);                     /* Use for in and out     */
  405.     return;
  406. }
  407. /****************************************************************************/
  408. /*                    Write char /attr at cursor position                   */
  409. /****************************************************************************/
  410. void set_char_atr(word atr_chr, register union REGS *bios)
  411. {
  412.     bios->h.ah=(byte) 0x09;                       /* Write char function    */
  413.     bios->h.al=(byte) atr_chr;                    /* Character              */
  414.     bios->h.bl=(byte) (atr_chr >> 8);             /* Attribute              */
  415.     bios->h.bh=(byte) 0x00;                       /* Page 0                 */
  416.     bios->x.cx=1;                                 /* One character          */
  417.     int86(VIDEO, bios, bios);                     /* Use for in and out     */
  418.     return;
  419. }
  420. /****************************************************************************/
  421. /*                   Get char /attr at cursor position                      */
  422. /****************************************************************************/
  423. word get_char_atr(register union REGS *bios)
  424. {
  425.     bios->h.ah=(byte) 0x08;                       /* Read char function     */
  426.     bios->h.bh=(byte) 0x00;                       /* Page 0                 */
  427.     int86(VIDEO, bios, bios);                     /* Use for in and out     */
  428.     return (bios->x.ax);                          /* Its in the AX regis    */
  429.  }
  430. /****************************************************************************/
  431. /*         Write a string with attributes at current position               */
  432. /****************************************************************************/
  433. void write_str(register char *string,
  434.                word atr,
  435.                register union REGS *bios)
  436. {
  437.     short row;
  438.     short col;
  439.     while (*string)                               /* Until the NULL         */
  440.     {
  441.         set_char_atr(atr|(short)*string++, bios); /* Write char and attr    */
  442.         get_curs(bios);                           /* Get cursor position    */
  443.         row = (short) bios->h.dh;                 /* Get row                */
  444.         col = (short) bios->h.dl;                 /* Get column             */
  445.         if (col > 69)                             /* Protect window         */
  446.             break;
  447.         set_curs (row,++col,bios);                /* Set column             */
  448.     }
  449.     return;
  450. }
  451. /****************************************************************************/
  452. /*    Substitutes for the enormous _puts (char *) runtime module            */
  453. /*                                                                          */
  454. /****************************************************************************/
  455. int puts(register const char *string)
  456. {
  457.     union REGS bios;
  458.     while (*string)
  459.     {
  460.         bios.h.al=(byte) *string++;               /* Character to print     */
  461.         bios.h.ah=(byte) 0x0E;                    /* Dumb terminal function */
  462.         bios.h.bh=(byte) 0x00;                    /* Page 0                 */
  463.         int86(VIDEO, &bios, &bios);               /* Use for in and out     */
  464.     }
  465.     bios.h.al=(byte) 0x0D;                        /* Character to print     */
  466.     bios.h.ah=(byte) 0x0E;                        /* Dumb terminal function */
  467.     bios.h.bh=(byte) 0x00;                        /* Page 0                 */
  468.     int86(VIDEO, &bios, &bios);                   /* Use for in and out     */
  469.  
  470.     bios.h.al=(byte) 0x0A;                        /* Character to print     */
  471.     bios.h.ah=(byte) 0x0E;                        /* Dumb terminal function */
  472.     bios.h.bh=(byte) 0x00;                        /* Page 0                 */
  473.     int86(VIDEO, &bios, &bios);                   /* Use for in and out     */
  474.     return(0);
  475. }
  476. /****************************************************************************/
  477. /*                          Print the 'Usage' prompt.                       */
  478. /*    Made complicated so we don't have to use the ENORMOUS _printf()       */
  479. /*    library file.  This saves a lot of space!                             */
  480. /*                                                                          */
  481. /****************************************************************************/
  482. void disp()
  483. {
  484.     puts(signon[2]);
  485.     puts(info);
  486.     return;
  487. }
  488. /****************************************************************************/
  489. /*                       E N D  O F  M O D U L E                            */
  490. /****************************************************************************/
  491.